//
//  MainTabBarController.m
//  Movie
//
//  Created by tengyang on 15/7/17.
//  Copyright (c) 2015年 tengyang. All rights reserved.
//

#import "MainTabBarController.h"
#import "MoreViewController.h"
#import "MovieViewController.h"
#import "NewsViewController.h"
#import "TopViewController.h"
#import "CinemaViewController.h"
#import "HWControl.h"
#import "BaseNavController.h"

@interface MainTabBarController ()

@end





@implementation MainTabBarController
- (void)_createSubView
{
    
    MovieViewController *v1=[[MovieViewController alloc]init];
    NewsViewController *v2=[[NewsViewController alloc]init];
    TopViewController *v3=[[TopViewController alloc]init];
    CinemaViewController *v4=[[CinemaViewController alloc]init];
    MoreViewController *v5=[[MoreViewController alloc]init];
    NSArray *array=@[v1,v2,v3,v4,v5];
    
    NSMutableArray *navArray=[[NSMutableArray alloc]init];
    for (int i=0; i<5; i++) {
        BaseNavController *nav=[[BaseNavController alloc]initWithRootViewController:array[i]];
        [navArray addObject:nav];
        
    }
    self.viewControllers=navArray;
    
    
}


- (void)_setTabBar
{
    //01
    
    
    for (UIView *subView in self.tabBar.subviews) {
        Class cls=NSClassFromString(@"UITabBarButton");
        if ([subView isKindOfClass:cls]) {
            [subView removeFromSuperview];
        }
       
    }
    
    
    //02设置背景图片
    [self.tabBar setBackgroundImage:[UIImage imageNamed:@"tab_bg_all"]];
    
    
    self.tabBar.translucent=YES;
    
    //03
    
    CGFloat width=self.tabBar.frame.size.width/5;
    CGFloat height=self.tabBar.frame.size.height;
    
    
    NSArray *imageName=@[@"movie_home.png",@"msg_new",@"start_top250",@"icon_cinema",@"more_setting"];
    
    NSArray *titleName=@[@"电影",@"新闻",@"Top",@"影院",@"更多"];
    
    
    _selectImage =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, width, height)];
    _selectImage.image=[UIImage imageNamed:@"selectTabbar_bg_all1"];
    [self.tabBar addSubview:_selectImage];
    
    for (int i=0; i<5; i++) {
        CGRect frame=CGRectMake(width*i, 0, width, height);
        HWControl *button=[[HWControl alloc]initWithFrame:frame withImageName:imageName[i] withTitle:titleName[i]];
        button.tag=i;
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.tabBar addSubview:button];
    }
    
    
    
    
}

- (void)buttonAction:(HWControl *)button
{
    self.selectedIndex=button.tag;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.3];
    _selectImage.center=button.center;
    [UIView commitAnimations];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self _createSubView];
    [self _setTabBar];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
